package huawei.dstack;

import java.util.Arrays;

public class JOffer38 {
    public static void main(String[] args){
        System.out.println(Arrays.toString(dailyTemperatures(new int[]{73,74,75,71,69,72,76,73})));
        System.out.println(Arrays.toString(dailyTemperatures(new int[]{30,40,50,60})));
        System.out.println(Arrays.toString(dailyTemperatures(new int[]{30,60,90})));
    }
    public static int[] dailyTemperatures(int[] temperatures) {
        int[] stack = new int[temperatures.length];//存储下标
        int stackLen = 0;
        int [] res = new int[temperatures.length];
        for (int i = temperatures.length-1; i >= 0; i--) {
            while (stackLen>0&&temperatures[stack[stackLen-1]]<=temperatures[i]){
                stackLen--;
            }
            res[i]=stackLen>0?(stack[stackLen-1]-i):0;
            stack[stackLen++]=i;
        }
        return res;
    }
}
